package com.atguigu.yjh.auth.controller;

import com.alibaba.fastjson.TypeReference;
import com.atguigu.yjh.auth.feign.MemberFerFeignService;
import com.atguigu.yjh.common.constant.AuthServerConstant;
import com.atguigu.yjh.common.vo.MemberRespVo;
import com.atguigu.yjh.auth.vo.UserLoginVo;
import com.atguigu.yjh.auth.vo.UserRegistVo;
import com.atguigu.yjh.common.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;

@Controller
public class LoginController {

    @Autowired
    MemberFerFeignService memberFerFeignService;




    @PostMapping("/regist")
    public  String regist(@Valid UserRegistVo vo, BindingResult result, Model model, RedirectAttributes redirectAttributes)
    {

        System.out.println("=================regist==================");
        System.out.println(vo);
//        if(result.hasErrors()){
//
//            Map<String, String> collect = result.getFieldErrors().stream().collect(Collectors.toMap(FieldError::getField,FieldError::getField));
////            model.addAttribute("errors",collect);
//            redirectAttributes.addAttribute("ereors",collect);
//            return "redirect:http://auth.asxyyjh.com/reg.html";
//        }

        R r = memberFerFeignService.regist(vo);
        if(r.getCode() == 0){
            return "redirect:http://auth.asxyyjh.com/login.html";
        }else {
            Map<String, String> errors = new HashMap<>();
            errors.put("msg",r.getData(new TypeReference<String>(){}));
            redirectAttributes.addFlashAttribute("errors",errors);
            return "redirect:http://auth.asxyyjh.com/reg.html";
        }


    }

    @PostMapping("/login")
    public String login(UserLoginVo vo, RedirectAttributes redirectAttributes, HttpSession session){
        System.out.println("=======================login====================");
        System.out.println(vo);

        R login = memberFerFeignService.login(vo);

        if(login.getCode()==0){
            MemberRespVo userLoginVo = memberFerFeignService.login1(vo);
            System.out.println(userLoginVo);


            System.out.println("===========login===========");
            System.out.println(login);
            session.setAttribute(AuthServerConstant.LOGIN_USER,userLoginVo);
            return "redirect:http://asxyyjh.com";
        }else {


//            Map<String,String> errors = new HashMap<>();
//            errors.put("msg",login.getData("msg",new TypeReference<String>(){}));
//            System.out.println("===================================");
//            System.out.println(errors);
            String errors="密码用户名错误";
           redirectAttributes.addFlashAttribute("errors",errors);
            return "redirect:http://auth.asxyyjh.com/login.html";
        }


    }


}


